package com.hbut.day01;

import java.util.Random;

/**
 * @author PJM
 * @date 2025/6/21-11:09
 */
public class Game {

    // JVM
    public static void main(String[] args) throws Exception{
        // 创建两个人
        Person p1 = new Person("张飞", 8000, 600, 1200, 3000);
        p1.setSkills(new Skill[]{new Skill("咆哮", 500, 300),
                new Skill("穿刺", 3000, 100), new Skill("疯魔乱舞", 3000, 1000)});
        Person p2 = new Person("邢道荣", 4000, 500, 800, 5000);
        p2.setSkills(new Skill[]{
                new Skill("吓汝一跳", 100, 500),
                new Skill("鹰扬虎视", 750, 200), new Skill("我是上将", 1050, 500)
        });
        Random rand = new Random();
        while (p1.getBlood()>0 && p2.getBlood()>0){
            // 获取两个人的随机值
            int num1 = rand.nextInt(100);
            int num2 = rand.nextInt(100);
            if (num1>num2){
                attack(p1, p2, rand);
                attack(p2, p1, rand);
            }else {
                attack(p2, p1, rand);
                attack(p1, p2, rand);
            }
            // 打完一次休息一会
            System.out.println("=======================================================");
            Thread.sleep(2000);
        }
        if (p1.getBlood()>0){
            System.out.println(p1.getName()+"对"+p2.getName()+"说：你这也不行啊！！！");
        }else {
            System.out.println(p2.getName()+"对"+p1.getName()+"说：你再练一练，再来吧！！！");
        }
    }

    public static void attack(Person p1,Person p2, Random rand){
        // 在攻击之前，要确保人是活着的
        if (p1.getBlood()>0){
            boolean flag = rand.nextBoolean();
            if (flag){
                int index = rand.nextInt(3);
                Skill skill = p1.getSkills()[index];
                if (p1.getMagic()>skill.getMagic()){
                    int hurt = skill.getHurt()+p1.getAttack() - p2.getDefense();
                    if (hurt>0){
                        System.out.println(p1.getName()+"发动技能："+skill.getName()+"对"+p2.getName()+"造成伤害："+hurt);
                        // 开始扣去蓝量
                        p1.setMagic(p1.getMagic()-skill.getMagic());
                        p2.acceptAttack(hurt);
                        System.out.println(p1.getName()+"打出真实伤害："+hurt);
                        System.out.println(p2.getName()+"收到伤害，血量剩余："+p2.getBlood());
                    }else {
                        System.out.println(p1.getName()+"发动技能："+skill.getName()+"，仍然无法对"+p2.getName()+"造成伤害！！！");
                    }
                }else {
                    // 普通的处理
                    System.out.println(p1.getName()+"气力不足，开始对"+p2.getName()+"发起普通攻击");
                    int hurt = p1.getAttack() - p2.getDefense();
                    if (hurt<0){
                        hurt = 0;
                        System.out.println(p1.getName()+"，没有造成实质性伤害，这是在闹吗？？");
                    }else {
                        System.out.println(p1.getName()+"打出真实伤害："+hurt);
                        p2.acceptAttack(hurt);
                        System.out.println(p2.getName()+"收到伤害，血量剩余："+p2.getBlood());
                    }
                }
            }else {
                System.out.println(p1.getName()+"开始对"+p2.getName()+"发起攻击");
                int hurt = p1.getAttack() - p2.getDefense();
                if (hurt<0){
                    hurt = 0;
                    System.out.println(p1.getName()+"，没有造成实质性伤害，这是在闹吗？？");
                }else {
                    System.out.println(p1.getName()+"打出真实伤害："+hurt);
                    p2.acceptAttack(hurt);
                    System.out.println(p2.getName()+"收到伤害，血量剩余："+p2.getBlood());
                }
            }
        }
    }

}
